package com.cll.controller.user;

import com.cll.dto.OrdersPaymentDTO;
import com.cll.dto.OrdersSubmitDTO;
import com.cll.result.PageResult;
import com.cll.result.Result;
import com.cll.service.OrderService;
import com.cll.vo.OrderPaymentVO;
import com.cll.vo.OrderSubmitVO;
import com.cll.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/user/order")
@Api(tags = "C端-订单相关接口")
public class OrderController {
    @Autowired
    private OrderService orderService;


    /**
     * 用户下单
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单",ordersSubmitDTO);
        //调用业务层处理业务
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        //返回数据给前端
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }


    /**
     * 用户分页查询历史订单
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @ApiOperation("查询用户历史订单")
    @GetMapping("/historyOrders")
    public Result<PageResult> historyOrders(Integer page, Integer pageSize, Integer status){
        log.info("查询用户历史订单: 页码{},每页大小{},订单状态",page,pageSize,status);
       PageResult pageResult = orderService.pageQuery(page,pageSize,status);
       return Result.success(pageResult);
    }

    /**
     * 用户催单
     * @param id
     * @return
     */
    @ApiOperation("用户催单")
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id) {
        log.info("用户催单:{}",id);
        orderService.reminder(id);
        return Result.success();
    }

    /**
     * 用户查询订单详情
     * @param id
     * @return
     */
    @ApiOperation("用户查询订单详情")
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> detail(@PathVariable Long id){
        log.info("用户查询订单详情:{}",id);
        OrderVO orderVO = orderService.detail(id);
        return Result.success(orderVO);
    }

    /**
     * 用户取消订单
     * @param id
     * @return
     */
    @ApiOperation("用户取消订单")
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable Long id) {
        log.info("用户取消订单:{}",id);
        orderService.cancel(id);
        return Result.success();
    }

    /**
     * 用户再来一单
     * @param id
     * @return
     */
    @ApiOperation("用户再来一单")
    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable Long id){
        log.info("用户再来一单:{}",id);
        orderService.repetition(id);
        return Result.success();
    }
}
